package com.jd.mysql.mgr.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jd.mysql.mgr.dao.MgrDbErrorDAO;
import com.jd.mysql.mgr.pojo.MgrAccount;
import com.jd.mysql.mgr.pojo.MgrDatabase;
import com.jd.mysql.mgr.pojo.MgrDbError;
import com.jd.mysql.mgr.util.StringUtil;

public class MgrDbErrorDAOImpl extends HibernateDaoSupport implements MgrDbErrorDAO{

	public void deleteMgrDbError(MgrDbError mdbe) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.delete(mdbe);
	}

	public MgrDbError findMgrDbErrorById(Integer id) {
		// TODO Auto-generated method stub
		return (MgrDbError) getHibernateTemplate().get(MgrDbError.class, id);
	}

	public void saveMgrDbError(MgrDbError mdbe) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.save(mdbe);
	}

	public void updateMgrDbError(MgrDbError mdbe) {
		// TODO Auto-generated method stub
		HibernateTemplate temp = this.getHibernateTemplate();
		temp.setFlushMode(HibernateTemplate.FLUSH_EAGER);
		temp.update(mdbe);
	}

	public List<MgrDbError> getMgrDbErrorList(Integer dbtype, Integer start,
			Integer limit) {
		// TODO Auto-generated method stub
		String hql = "from MgrDbError where 1=1 ";
		if(dbtype!=-1){
			hql+="and dbtype=:dbtype ";
		}
		hql+="order by id desc";
		Session session = this.getSession();
		Query query  = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		if(dbtype!=-1){
			query.setInteger("dbtype", dbtype);
		}
		List<MgrDbError> list = query.list();
		return (list==null || list.size()==0)?null:list;
	}

	public Long getMgrDbErrorCount(Integer dbtype) {
		// TODO Auto-generated method stub
		Long totalCount=0l;
		String hql = "select count(*) as count from MgrDbError where 1=1 ";
		if(dbtype!=-1){
			hql+="and dbtype=:dbtype ";
		}
		Session session = this.getSession();
		Query query  = session.createQuery(hql);
		if(dbtype!=-1){
			query.setInteger("dbtype", dbtype);
		}
		List<Long> list = query.list();
		if(list!=null && list.size()==1){
			totalCount = list.get(0);
		}
		return totalCount;
	}

}
